package com.ndd.interceptor;

import com.ndd.constant.Constants;
import com.ndd.enums.ServiceCode;
import com.ndd.exception.ServiceException;
import com.ndd.utils.JwtUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @ProjectName: cmall-ybu
 * @Titile: TokenInterceptorHandler
 * @Author: Lucky
 * @Description: token拦截器
 */

public class TokenInterceptorHandler implements HandlerInterceptor {
    /**
     * 前置拦截处理
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //从请求头中获取token
        String token = request.getHeader(Constants.TOKEN_HEADER);
        if(token==null || token == ""){
            throw new ServiceException(ServiceCode.ERR_JWT_LOGOUT,"token不能为空");
        }
        //校验
        JwtUtils.verifyToken(token);
        //说明token合法的
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }
}
